﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.XSchEmA.Aspect;
using Pixysoft.Framework.XSchEmA.Entity;
using System.Data.OleDb;
using Pixysoft.Framework.XSchEmA.Sql;
using Pixysoft.Framework.XSchEmA.Database;
using System.Data.SqlClient;

namespace Pixysoft.Framework.XSchEmA
{
    class SqlServerSchema : IDbSchema
    {
        ISchemaAspect aspect = new SqlServerSchemaAspect();

        class SqlServerDbMetadata : DbMetadata
        {
            public SqlServerDbMetadata(ISchemaAspect aspect)
                : base(aspect)
            {
            }

            public override DatabaseSchema GetSchema(DatabaseInfo info)
            {
                lock (typeof(SqlServerDbMetadata))
                {
                    SqlServerSchemaDownloader downloader = new SqlServerSchemaDownloader();

                    DatabaseSchema schema = new DatabaseSchema(info);

                    SqlConnection oconn = new SqlConnection();
                    oconn.ConnectionString = info.ConnectionString;
                    oconn.Open();

                    downloader.GetTables(info.DatabaseName, oconn, schema);
                    downloader.GetColumns(info.DatabaseName, oconn, schema);
                    schema.BuildColumns();

                    downloader.GetIdentifyColumns(info.DatabaseName, oconn, schema);
                    schema.BuildColumns();

                    downloader.GetConstraints(oconn, schema);
                    schema.BuildPrimaryKeys();
                    schema.BuildUniques();

                    downloader.GetIndexes(oconn, schema);
                    schema.BuildIndexs();

                    downloader.GetForeignKeys(oconn, schema);
                    schema.BuildForeignKeys();

                    oconn.Close();
                    oconn.Dispose();

                    schema.Inspection();

                    return schema;
                }
            }

            public override bool Commit(DatabaseInfo info, DatabaseSchema schema)
            {
                return SqlServerSchemaUploader.Instance(info).CreateSchema(schema)
                    && SqlServerSchemaUploader.Instance(info).CreateAutoIncrement(schema);
            }

            public override bool Clear(DatabaseInfo info, DatabaseSchema schema)
            {
                return SqlServerSchemaUploader.Instance(info).ClearSchema(schema);
            }

            public override bool Clear(ISchemaCommand command, DatabaseSchema schema)
            {
                return new SqlServerCommonSchemaUploader(command).ClearSchema(schema);
            }

            public override bool CreateAutoIncrement(DatabaseInfo info, ColumnSchema column)
            {
                return SqlServerSchemaUploader.Instance(info).CreateAutoIncrement(column);
            }

            public override bool Commit(ISchemaCommand command, DatabaseSchema schema)
            {
                return new SqlServerCommonSchemaUploader(command).CreateSchema(schema)
                && new SqlServerCommonSchemaUploader(command).CreateAutoIncrement(schema);
            }


        }

        public IDbVerifier Verifier
        {
            get { return new DbVerifier(aspect); }
        }
        public IDbModifier Modifier
        {
            get { return new DbModifier(aspect); }
        }
        public IDbChecker Checker
        {
            get { return new DbChecker(aspect); }
        }
        public IDbHinter Hinter
        {
            get { return new DbHinter(aspect); }
        }
        public IDbMetadata Schema
        {
            get { return new SqlServerDbMetadata(aspect); }
        }
        public ISqlBuilder SqlBuilder
        {
            get { return aspect.SqlBuilder; }
        }
        public IDbModifier GetRealModifier(ISchemaCommand command)
        {
            return new DbRealModifier(aspect, command);
        }
    }
}
